package apt.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

import apt.bean.RoleBean;
import apt.bean.UserBean;
import apt.dao.RoleDao;
import apt.dao.UserDao;

public class UserAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private UserBean userBean;
	private List<UserBean> userList;
	private int userId;
	private ArrayList<RoleBean> listRole;

	private UserDao userDao = new UserDao();

	public String showFormAddUser() {
		initData();
		return SUCCESS;
	}

	public String deleteUser()
	{
		userDao.deleteUser(userBean);
		return SUCCESS;
	}
	
	public void initData() {
		listRole = new RoleDao().getRole();
	}

	public ArrayList<RoleBean> getListRole() {
		return listRole;
	}

	public void setListRole(ArrayList<RoleBean> listRole) {
		this.listRole = listRole;
	}

	public String fillDataUser() {
		try {
			initData();
			userBean = userDao.getUserByID(userId);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}

		return SUCCESS;
	}

	public String addUser() {
		if (userDao.addUser(userBean)) {
			return SUCCESS;
		} else
			return ERROR;
	}

	public String getUser() {
		try {
			userList = userDao.getListUser();
			for (int i = 0; i < userList.size(); i++) {
				System.out.println(userList.get(i).getUserName());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	public String updateUser() {
		if (!userDao.updateUser(userBean)) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String deleteUser(int id) {
		if (!userDao.deleteUser(userBean)) {
			return ERROR;
		}
		return SUCCESS;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}

	public List<UserBean> getUserList() {
		return userList;
	}

	public void setUserList(List<UserBean> userList) {
		this.userList = userList;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public UserBean getUserBean() {
		return userBean;
	}

}
